﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using CFSystem.JXC.BLL;
using CFSystem.JXC.Model;

namespace CFSystem.JXC.BasisInfo
{
    public partial class FrmInventorySearch : Form
    {
        public FrmInventorySearch()
        {
            InitializeComponent();
            loadAllInfo();
            LoadTreeCondition();
            AddFilterCondition();

        }
        /// <summary>
        /// add the condition to filter the search results.
        /// </summary>
        private void AddFilterCondition()
        {
            cmbSearchCondition.Items.Add(">");
            cmbSearchCondition.Items.Add("<");
            cmbSearchCondition.Items.Add("=");
            cmbSearchCondition.Items.Add(">=");
            cmbSearchCondition.Items.Add("<=");
        }

        private void LoadTreeCondition()
        {
            #region construct columns
            //initial grid
            DataTable dt = new DataTable();
            //Create a new column for datatable.
            DataColumn dc = new DataColumn("编号", System.Type.GetType("System.String"));
            //Add created column to datatable object.
            dt.Columns.Add(dc);
            //Create a new column for datatable.
            dc = new DataColumn("类别名称", System.Type.GetType("System.String"));
            //Add created column to datatable object.
            dt.Columns.Add(dc);
            //Create a new column for datatable.
            dc = new DataColumn("隶属于", System.Type.GetType("System.String"));
            //Add created column to datatable object.
            dt.Columns.Add(dc);
            #endregion

            DataRow dr;

            ProdCatergory PC = new ProdCatergory(); //BLL calling.
            IList<ProdCatergoryInfo> IPCInfo = PC.GetProdCatergorys();
            foreach (ProdCatergoryInfo PCI in IPCInfo)
            {
                dr = dt.NewRow();
                dr["编号"] = PCI.ProdCatergoryID.ToString();
                dr["类别名称"] = PCI.ProdCatergoryName.ToString();
                dr["隶属于"] = PCI.ProdCatergoryParentName.ToString();
                dt.Rows.Add(dr);
            }

            TreeViewDisplay(dt);
            TVDisplay.AfterSelect += new TreeViewEventHandler(TVDisplay_AfterSelect);
        }
        /// <summary>
        /// 当用户点击左侧树节点时，触发该事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TVDisplay_AfterSelect(object sender, EventArgs e)
        {

            string condition = string.Empty;

            if (TVDisplay.SelectedNode.Text.Trim() != "所有类别")
            {
                condition = " and b.[Name] ='" + TVDisplay.SelectedNode.Text.Trim() + "'";
            }

            StorageInfoSearch sis = new StorageInfoSearch();
            IList<StorageInfo> storageInfo = sis.SearchStorageRecords(condition);
            dgvStorageInfo.DataSource = storageInfo;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dtCatergory"></param>
        private void TreeViewDisplay(DataTable dtCatergory)
        {
            try
            {
                TreeNode rootnode = new TreeNode();
                rootnode.Text = "所有类别";
                TVDisplay.Nodes.Clear();
                TVDisplay.Nodes.Add(rootnode);
                TreeViewDisplay(dtCatergory, "所有类别", rootnode);
                TVDisplay.ExpandAll();
                TVDisplay.SelectedNode = new TreeNode("所有类别");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="catTable"></param>
        /// <param name="ParentID"></param>
        /// <param name="tn"></param>
        private void TreeViewDisplay(DataTable catTable, string ParentID, TreeNode tn)
        {
            DataRow[] dt = catTable.Select("隶属于='" + ParentID + "'"); //find childs by parentID.

            foreach (DataRow dr in dt)
            {
                //if (dr["parentID"] != null)
                if (dr["类别名称"] != null) //has child ,added, -digui condition.
                {
                    TreeNode child = new TreeNode();
                    child.Text = dr["类别名称"].ToString();
                    tn.Nodes.Add(child);
                    TreeViewDisplay(catTable, dr["类别名称"].ToString(), child); //typeID is the parent of next node digui.
                }
            }
        }

        private void loadAllInfo()
        {
            StorageInfoSearch sis = new StorageInfoSearch();
            IList<StorageInfo> storageInfo = sis.GetAllStorageRecords();
            dgvStorageInfo.DataSource = storageInfo;
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string condition = string.Empty;

            //check.
            if ((TxtStorageAmount.Text.Trim() == string.Empty
                && cmbSearchCondition.Text.Trim() != string.Empty)
                || (TxtStorageAmount.Text.Trim() != string.Empty
                && cmbSearchCondition.Text.Trim() == string.Empty))
            {
                MessageBox.Show("请设定完整的查询条件!");
                cmbSearchCondition.Focus();
                return;
            }
            else if (TxtStorageAmount.Text.Trim() != string.Empty
                       && cmbSearchCondition.Text.Trim() != string.Empty)
            {
                condition = " and a.[StockAmount]" + cmbSearchCondition.Text.Trim() + TxtStorageAmount.Text.Trim();
            }
            else
            {
                //both empty.
            }

            if (txtProdSearchKey.Text.Trim() != string.Empty && condition != string.Empty)
            {
                condition += " and a.[ProductID] ='" + txtProdSearchKey.Text.Trim() + "'";
            }
            else if (txtProdSearchKey.Text.Trim() == string.Empty && condition == string.Empty)
            {
                condition = string.Empty;
            }
            else if (txtProdSearchKey.Text.Trim() == string.Empty && condition != string.Empty)
            {
                //only first condition.
            }
            else
            {
                condition = " and a.[ProductID] ='" + txtProdSearchKey.Text.Trim() + "'";
            }

            StorageInfoSearch sis = new StorageInfoSearch();
            IList<StorageInfo> storageInfo = sis.SearchStorageRecords(condition);
            dgvStorageInfo.DataSource = storageInfo;

        }
    }
}
